package com.shaoshaohuo.app.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.adapter.EvaluationListAdapter;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.constant.ListViewAction;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.Evaluation;
import com.shaoshaohuo.app.entity.EvaluationListEntity;
import com.shaoshaohuo.app.entity.EvaluationListEntity.EvaluationList;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.view.TopbarView;
import com.shaoshaohuo.app.view.XListView;
import com.shaoshaohuo.app.view.XListView.IXListViewListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的评价
 */
public class OtherEvaluationActivity extends BaseActivity implements IXListViewListener, View.OnClickListener {

    private TopbarView mTopbarView;

    private TextView tv_evaluation_all;
    private TextView tv_evaluation_good;
    private TextView tv_evaluation;
    private TextView tv_evaluation_bad;

    private XListView mListView;


    private List<Evaluation> mDataList = new ArrayList<Evaluation>();
    private int size = 10;
    private String action = ListViewAction.NEW;
    private String cursor = "";
    private String dtype = null;  // 评论等级： 1差评，2中评，3好评
    private String orderid;

    private EvaluationListAdapter<Evaluation> mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other_evaluation);

        initData();
        initView();
        setUpView();
        requestData(true);
    }

    private void initData() {
        orderid = getIntent().getStringExtra(ExtraName.orderid);
    }

    private void requestData(boolean isLoading) {
        if (isLoading) {
            startLoadingDialog();
        }
        RequestService.getInstance().getEvaluationList(this, null, orderid, dtype, size + "", cursor, action, EvaluationListEntity.class,
                new RequestListener() {

                    @Override
                    public void onSuccess(int requestCode, BaseEntity resultData) {
                        dismissLoadingDialog();
                        if (resultData.isOk()) {
                            EvaluationListEntity entity = (EvaluationListEntity) resultData;

                            EvaluationList data = entity.getData();
                            List<Evaluation> list = data.getList();
                            mDataList.addAll(list);

                            tv_evaluation_all.setText("全部\n"+data.getStartnum());
                            tv_evaluation_good.setText("好评\n"+data.getStartnumgood());
                            tv_evaluation.setText("中评\n"+data.getStartnummiddle());
                            tv_evaluation_bad.setText("差评\n"+data.getStartnumbad());

                            if ((mDataList.size() < size) || (action.equals(ListViewAction.HISTORY) && list.size() < size)) {
                                mListView.endLoadingMore("没有更多了");
                            }
                            setAdapter(mDataList);

                        } else {
                            showToast(resultData.getMsg());
                        }
                        onLoad();
                    }

                    @Override
                    public void onFailed(int requestCode, Exception error, String msg) {
                        dismissLoadingDialog();
                        showToast(R.string.please_check_network);
                        onLoad();
                    }
                });
    }

    private void initView() {
        mTopbarView = (TopbarView) this.findViewById(R.id.topbar);
        mListView = (XListView) this.findViewById(R.id.listview_evaluation_list);

        tv_evaluation_all = (TextView) findViewById(R.id.tv_evaluation_all);
        tv_evaluation_good = (TextView) findViewById(R.id.tv_evaluation_good);
        tv_evaluation = (TextView) findViewById(R.id.tv_evaluation);
        tv_evaluation_bad = (TextView) findViewById(R.id.tv_evaluation_bad);
    }

    private void setUpView() {
        mTopbarView.setCenterText("我的评价");
        mTopbarView.setLeftView(true, true);

        tv_evaluation_all.setOnClickListener(this);
        tv_evaluation_good.setOnClickListener(this);
        tv_evaluation.setOnClickListener(this);
        tv_evaluation_bad.setOnClickListener(this);

        tv_evaluation_all.setSelected(true);


        setListView();
    }

    protected void setAdapter(List<Evaluation> list) {


        if (mAdapter == null) {
            mAdapter = new EvaluationListAdapter<Evaluation>(this, list, false);
            mListView.setAdapter(mAdapter);
        } else {
            mAdapter.setData(list);
        }
    }

    private void setListView() {
        mListView.setPullLoadEnable(true);
        mListView.setPullRefreshEnable(true);
        mListView.setXListViewListener(this);
    }


    @Override
    public void onRefresh() {
        if (mDataList.size() > 0) {
            cursor = mDataList.get(0).getCursor();
        } else {
            cursor = "";
        }
        action = ListViewAction.NEW;
        mListView.setPullLoadEnable(true);
        requestData(false);
    }

    @Override
    public void onLoadMore() {
        if (mDataList.size() > 0) {
            cursor = mDataList.get(mDataList.size() - 1).getCursor();
        } else {
            cursor = "";
        }
        action = ListViewAction.HISTORY;
        requestData(false);
    }

    private void onLoad() {
        mListView.stopRefresh();
        mListView.stopLoadMore();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_evaluation_all:
                changeType(null);
                tv_evaluation_all.setSelected(true);
                break;
            case R.id.tv_evaluation_good:
                changeType("3");
                tv_evaluation_good.setSelected(true);
                break;
            case R.id.tv_evaluation:
                changeType("2");
                tv_evaluation.setSelected(true);
                break;
            case R.id.tv_evaluation_bad:
                changeType("1");
                tv_evaluation_bad.setSelected(true);
                break;
        }
    }

    private void changeType(String type) {
        if (dtype != type) {
            dtype = type;
            mDataList.clear();
            requestData(true);

            tv_evaluation_all.setSelected(false);
            tv_evaluation_good.setSelected(false);
            tv_evaluation.setSelected(false);
            tv_evaluation_bad.setSelected(false);

        }
    }
}
